package public

import (
	"context"

	"server/internal/svc"
	"server/internal/types"

	"github.com/mojocn/base64Captcha"

	"github.com/zeromicro/go-zero/core/logx"
)

type MpCaptchaLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewMpCaptchaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MpCaptchaLogic {
	return &MpCaptchaLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *MpCaptchaLogic) MpCaptcha() (resp *types.MpCaptchaResp, err error) {
	var store = base64Captcha.DefaultMemStore
	driver := base64Captcha.NewDriverDigit(80, 240, 6, 0.7, 80)
	c := base64Captcha.NewCaptcha(driver, store)
	id, b64s, answer, err := c.Generate()
	if err != nil {
		return nil, err
	}

	logx.Infof("answer:%s", answer)

	return &types.MpCaptchaResp{
		CaptchaKey:    id,
		CaptchaBase64: b64s,
	}, nil
}
